import { EVENT_NAME_ENUM, BUNDLE_TYPE_ENUM } from "../base/EnumIndex";
import { EventManager } from "../manager/EventManager";
import { ResManager1 } from "../manager/ResManager1";


const { ccclass, property } = cc._decorator;

@ccclass
export default class SliderItem extends cc.Component {
    @property(cc.ProgressBar)
    progressBar: cc.ProgressBar = null;

    @property({ type: cc.Sprite, displayName: "进图条头" })
    head: cc.Sprite = null;

    onLoad() {
        EventManager.ins.on(EVENT_NAME_ENUM.UPDATE_SLIDER, this.setProgress, this);
    }
    protected onDestroy(): void {
        EventManager.ins.off(EVENT_NAME_ENUM.UPDATE_SLIDER, this.setProgress, this);
    }
    /**  */
    async updateHead(headUrl: string) {
        if (headUrl === "" || !headUrl) {
            this.head.node.active = false;
        } else {
            let spf = await ResManager1.ins.loadRes(headUrl, cc.SpriteFrame, BUNDLE_TYPE_ENUM.LEVEL);
            this.head.spriteFrame = spf;
        }
    }
    /** 直接设置某个进度值 */
    setProgress(progress: number) {
        this.progressBar.progress = progress;
        this.updateHeadX();
    };
    /** 刷新头位置 */
    updateHeadX() {
        let width = this.progressBar.barSprite.node.width;
        this.head.node.x = width * this.progressBar.progress;
    };
}